#include "audio.h"

//==============================================================================DATA
switches_s switches = {FALSE};
entry_s *now_playback = NULL;
control_e control = GONE;
status_s status = { 0 };


static void read_key_value();
static void save_key_value();
//==============================================================================FUNCTION
int audio_start()
{
	read_key_value();
	plugin_install();
	
	int n = lists_read();
	if( n == 0 ){ cur_list = 0; return 0; }
	if( cur_list > n-1 ) cur_list = 0;
	now_playback = CUR_ENTRY;

	return EXIT_SUCCESS;
}

static void read_key_value()
{
	char *value;
	
	main_config_get( "[audio]cur_list", &value );
	if( !strcmp( value, "_NOTHING_" ) ) value = "0";
	sscanf( value, "%d", &cur_list );

	main_config_get( "[output]using", &value );
	if( !strcmp( value, "_NOTHING_" ) ) value = context.output;
	output_using = value;

}

static void save_key_value()
{
	char value[VALUE_SIZE];
	
	sprintf( value, "%d", cur_list);
	main_config_set( "[audio]cur_list", value);

	strcpy( value, output_using );
	main_config_set( "[output]using", value);
}


//==============================================================================COMMAND HANDLER
void handle_C_EXIT( void )
{
	save_key_value();
	lists_save();
	
	if(output.handle)
		output.close();
	plugin_uninstall();
	
	fprintf( stderr, "\nAudio Exit\n" );
	pthread_exit( NULL );
}

void handle_C_PLAYBACK( void )
{
	int list_temp=0, entry_temp=0, i=0;

	if( 0 == end_list ) goto out;
	if( 0 == CUR_LIST.total_entries_number ) goto out;
	
	if( audio_command.playback.relative_on ){

		entry_temp = CUR_LIST.sequence[ CUR_LIST.cur_entry ] + audio_command.playback.file_offset;
		while( CUR_LIST.sequence[i] != entry_temp ) i++;
		entry_temp = i;
		
		if( (CUR_LIST.sequence[entry_temp] >= 0)  &&  (entry_temp < CUR_LIST.total_entries_number)  ){	
			CUR_LIST.cur_entry = entry_temp;
		}
		else goto out;
		
	}else{

		list_temp = audio_command.playback.list_offset;
		while( CUR_LIST.sequence[i] != audio_command.playback.file_offset ) i++;
		entry_temp = i;
		
		if( (list_temp >= 0)  &&  (list_temp < end_list)  &&  (entry_temp >= 0)  &&  (entry_temp < list[list_temp].total_entries_number)  ){
			cur_list = list_temp;
			CUR_LIST.cur_entry = entry_temp;
		}
		else goto out;
	}

	now_playback = CUR_ENTRY;
	control = START;
	switches.show_on = TRUE;
	
out: ;
}

void handle_C_PAUSE( void )
{
	if( status.flags & PLAYING ){
		control = PAUSE;
	}
}

void handle_C_STOP( void )
{	
	control = STOP;
}

void handle_C_VOLUME( void )
{
	if( audio_command.volume.relative_on ){
		if( (status.volume + audio_command.volume.volume_offset) > 0 &&
		    (status.volume + audio_command.volume.volume_offset) < 999)
			status.volume += audio_command.volume.volume_offset;
	}else{
			status.volume = audio_command.volume.volume_offset;
	}
	status.mute_on = audio_command.volume.mute_on;
	
	control = VOLUME;
}

void handle_C_SEEK( void )
{
	if( audio_command.seek.relative_on ){
		if( (status.position + audio_command.seek.seek_offset) > 0 &&
		    (status.position + audio_command.seek.seek_offset) < 999)		   
			status.position += audio_command.seek.seek_offset;
	}else{
		status.position = audio_command.seek.seek_offset;
	}
	
	control = SEEK;
}

void handle_C_LIST_ADD( void )
{
	_list_add( audio_command.list_add.dir_name, audio_command.list_add.list_name );
}

void handle_C_LIST_DEL( void )
{
	_list_del( audio_command.list_del.offset );
}

void handle_C_LIST_CLEAR_STATUS( void )
{
	status.flags &= ~ADDING;
	status.flags &= ~DELING;
}

void handle_C_CHANGE_OUTPUT( void )
{
	output.close();
	plugin_output_uninstall();
	output_using = audio_command.change_output.name;
	plugin_output_install();
	output.open();
}

